My refresh function works well, and will update the display to the next digit each time I call it. If I could call this function at regular intervals it would refresh each digit in turn. In the gaps between calls the current active digit would be lit.
It turns out that the PICmicro can do exactly what we need to make our refresh function run at regular intervals. It does this by means of interrupts. Interrupts are not part of C. They are provided by most computer systems, but are not usually for use by the faint hearted. Fortunately, thanks to the author of BoostC we can use them on the PICmicro very easily. Note that the way that interrupts are actually used is not the same on all processors, we are going to learn the PICmicro way of doing them.
An interrupt is a signal which causes the processor to stop what it is doing and go off and run an interrupt handler. This is a special piece of code which you write to deal with the cause of an interrupt. The IBM PC has a number of interrupt connections, each attached to a particular event. There is one which is invoked when a key is pressed on the keyboard, and another runs when a character arrives down the RS232 (serial) port. The PICmicro only supports a single interrupt, but you can control what causes the interrupt to occur. This can be an external event such as a change of state on one of the input pins or an internal event such as the timer overflowing.
After a reset the PICmicro interrupts are turned off, i.e. no interrupts are acted on and no interrupt handlers are called.You make the PICmicro respond to interrupts by setting control bits to switch interrupts on. The bits live in OPTION and INTCON registers.
When the PICmicro gets an interrupt it jumps to a particular location in the program memory. The BoostC compiler has been written so that this is automatically taken care of, all we have to do is provide a function called interrupt and this will be called when the interrupt occurs. If we were writing assembler we would have to worry about stacking registers and making sure we return from the interrupt in the correct way, not to mention the business of attaching the code to the interrupt itself.
